package org.intrasearch.frontend.views;

import com.google.inject.Inject;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.PropertyModel;
import org.intrasearch.core.Artifact;
import org.intrasearch.core.ArtifactSearcher;

import java.util.List;

public class MainSearchPage extends MasterPage
{
    private String query;
    private ResultsPanel resultsPanel;
    @Inject
    public ArtifactSearcher artifactSearcher;

    public MainSearchPage()
    {
        super();
        init();
    }

    private void init()
    {
        PropertyModel<String> stringPropertyModel = new PropertyModel<String>(this, "query");
        resultsPanel = new ResultsPanel("resultsPanel", new AbstractReadOnlyModel<List<Artifact>>()
        {
            @Override
            public List<Artifact> getObject()
            {
                return query != null ? artifactSearcher.searchQuery(query) : null;
            }
        });
        resultsPanel.setVisible(false);
        add(resultsPanel);
        Form<?> form = new Form("form");
        form.add(new TextField<String>("queryInput", stringPropertyModel));
        add(form);
    }

    public String getQuery()
    {
        return query;
    }

    public void setQuery(String query)
    {
        this.query = query;
        resultsPanel.setVisible(query != null);
    }
}
